package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CUserFoodMatchForm;
import org.lalib.controller.form.CUserFoodMatchForm.FoodMatchBean;
import org.lalib.controller.service.CFoodService;
import org.lalib.controller.service.CUserService;
import org.lalib.entity.object.EFood;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.EUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cUserFoodMatchAction")
public class CUserFoodMatchAction implements IControllerAction {

	@Autowired
	private CUserFoodMatchForm cUserFoodMatchForm;

	@Autowired
	private CFoodService cFoodService;

	@Autowired
	private CUserService cUserService;

	private static final long serialVersionUID = 1L;

	public String init(EItem item) {
		EUser user = (EUser) item;
		List<EFood> allFoods = cFoodService.getAllFoods();
		resetVariables(user, allFoods);
		return "userFoodMatchDefination";
	}

	private void resetVariables(EUser user, List<EFood> allFoods) {
		user = cUserService.getUserById(user.getId());
		List<EFood> includedFoods = user.getFoods();
		List<EFood> excludedFoods = filterFoods(allFoods, includedFoods);
		cUserFoodMatchForm.setExcludedFoods(getFoodMatchBeanFromFoods(excludedFoods));
		cUserFoodMatchForm.setIncludedFoods(getFoodMatchBeanFromFoods(includedFoods));
		cUserFoodMatchForm.setUser(user);
		cUserFoodMatchForm.setAllFoods(allFoods);
	}

	private List<FoodMatchBean> getFoodMatchBeanFromFoods(List<EFood> foods) {
		List<FoodMatchBean> foodMatchBeans = new ArrayList<>();
		for (EFood eFood : foods)
			foodMatchBeans.add(cUserFoodMatchForm.new FoodMatchBean(eFood));
		return foodMatchBeans;
	}

	private List<EFood> filterFoods(List<EFood> allFoods, List<EFood> includedFoods) {
		List<EFood> excludedFoods = new ArrayList<>();
		for (EFood food : allFoods) {
			boolean check = true;
			for (EFood eFood : includedFoods) {
				if (eFood.getId().equals(food.getId())) {
					check = false;
					break;
				}
			}
			if (check) {
				excludedFoods.add(food);
			}
		}
		return excludedFoods;
	}

	public void addToUser() {
		List<FoodMatchBean> excludedBeans = cUserFoodMatchForm.getExcludedFoods();
		EUser user = cUserFoodMatchForm.getUser();
		user = cUserService.getUserById(user.getId());
		boolean check = false;
		for (FoodMatchBean foodMatchBean : excludedBeans) {
			if (foodMatchBean.isSelected()) {
				user.getFoods().add(foodMatchBean.geteFood());
				check = true;
			}
		}

		if (check) {
			cUserService.update(user);
			resetVariables(user, cUserFoodMatchForm.getAllFoods());
		}
	}

	public void removeFromUser() {
		List<FoodMatchBean> selectedBeans = cUserFoodMatchForm.getIncludedFoods();
		EUser user = cUserFoodMatchForm.getUser();
		user = cUserService.getUserById(user.getId());
		boolean check = false;

		for (FoodMatchBean foodMatchBean : selectedBeans) {
			if (foodMatchBean.isSelected()) {
				deleteUserFoodForFood(user, foodMatchBean.geteFood());
				check = true;
			}
		}

		if (check) {
			cUserService.update(user);
			resetVariables(user, cUserFoodMatchForm.getAllFoods());
		}
	}

	private void deleteUserFoodForFood(EUser user, EFood food) {
		int id = 0;
		boolean found = false;

		for (int i = 0; i < user.getFoods().size(); i++) {
			if (user.getFoods().get(i).getId().equals(food.getId())) {
				id = i;
				found = true;
			}
		}
		if (found)
			user.getFoods().remove(id);
	}
}
